package com.jule.chat.config;

import com.jule.core.configuration.ConfigurableProcessor;
import com.jule.core.configuration.Property;
import com.jule.core.utils.PropertiesUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Properties;

/**
 * @author xujian
 */
public class Config {

	protected static final Logger log = LoggerFactory.getLogger(Config.class);

	@Property(key = "chatserver.bind.port", defaultValue = "8080")
	public static int BIND_PORT;

	/**
	 * Load configs from files.
	 */
	public static void load() {
		try {

			Properties myProps = null;
			try {
				log.info("Loading: mycs.properties");
				myProps = PropertiesUtils.load("./config/mycs.properties");
			}
			catch (Exception e) {
				log.info("No override properties found");
			}

			Properties[] props = PropertiesUtils.loadAllFromDirectory("./config");
			PropertiesUtils.overrideProperties(props, myProps);

			log.info("Loading: chatserver.properties");
			ConfigurableProcessor.process(Config.class, props);
		}
		catch (Exception e) {
			log.error("Can't load chatserver configuration", e);
			throw new Error("Can't load chatserver configuration", e);
		}
	}
}
